cards = [
    {'name': '周冠全', 'phone': '15976544161', 'QQ': '2157779530', 'email': '2157779530@qq.com'},
    {'name': 'mia', 'phone': '15976374829', 'QQ': '2164284730', 'email': '3242579530@qq.com'},
    {'name': 'jack', 'phone': '24323374829', 'QQ': '224228230', 'email': '592324530@qq.com'}
]


def menu():
    print("*" * 30)
    s = """欢迎使用【名片管理系统】V1.0
    1.新建名片
    2.显示全部
    3.查询名片
    0.退出系统"""
    print(s)
    print("*" * 30)


def add_card(name, phone, QQ, email):
    dic = {'name': name, 'phone': phone, 'QQ': QQ, 'email': email}
    cards.append(dic)


def query(kw):
    for card in cards:
        for k, v in card.items():
            if kw == v:
                return card
    return False


def modify_card(dic, name, phone, QQ, email):
    dic['name'] = name
    dic['phone'] = phone
    dic['QQ'] = QQ
    dic['email'] = email


def del_card(target_card):
    # 通过遍历查找匹配的字典（通过内存地址判断）
    for i, card in enumerate(cards):
        if id(card) == id(target_card):
            del cards[i]
            print("删除成功！")

    # if target_card in cards:
    #     cards.remove(target_card)
    #     print("删除成功！")


def quit1():
    print("欢迎再次使用【名片管理系统】！")


menu()
while True:
    choice = input("请输入您的选择：")
    if choice == '1':
        name = input("请输入名字：")
        phone = input("请输入电话号码：")
        QQ = input("请输入QQ号：")
        email = input("请输入入邮箱：")
        add_card(name, phone, QQ, email)
        print("添加成功！")
    elif choice == '2':
        for card in cards:
            print(card)
            # for k, w in card.items():
            #     print(k, w)
    elif choice == '3':
        kw = input("请输入你要查询的关键字：")
        result = query(kw)
        if result:
            print(result)
            op = input("输入4修改名片，输入5删除名片,输入6退出：")
            if op == '4':
                name = input("请输入名字：")
                phone = input("请输入电话号码：")
                QQ = input("请输入QQ号：")
                email = input("请输入入邮箱：")
                modify_card(result, name, phone, QQ, email)
                print("修改成功！")
            elif op == '5':
                del_card(result)
            else:
                break
        else:
            print("没有查询到相关信息！")
    elif choice == '0':
        quit1()
        break
    else:
        print("请重新输入！")
